﻿using System;
using System.Linq;
using System.Windows.Input;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using GalaSoft.MvvmLight.Command;
using Mipic.Infrastructure;
using Mipic.Infrastructure.Services;

namespace Mipic.Modules.Interactions.ViewModels
{
    /// <summary>
    /// ViewModel for <see cref="Interactions.Views.ConfigurationView"/>
    /// </summary>
    public class ConfigurationViewModel : NotificationObject
    {
        
        #region //--------- Private Fields --------------//
        
        private readonly IRegionManager _regionManager;
        private readonly IOpenFileDialogService _dialog;
        
        private string _selectedImagePath = String.Empty;
        
        #endregion
        
        #region //--------- Public Constructors ---------//
        
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationViewModel" /> class.
        /// </summary>
        /// <param name="dialog">The dialog.</param>
        public ConfigurationViewModel(IOpenFileDialogService dialog, IRegionManager regionManager)
        {
            _regionManager = regionManager;
            _dialog = dialog;
        }
        
        #endregion
        
        #region //--------- CLR Properties --------------//
        
        /// <summary>
        /// Gets or sets the selected image path.
        /// </summary>
        /// <value>The selected image path.</value>
        public string SelectedImagePath
        {
            get
            {
                return _selectedImagePath;
            }
            set
            {
                if (_selectedImagePath != value)
                {
                    _selectedImagePath = value;
                    RaisePropertyChanged(() => this.SelectedImagePath);
                }
            }
        }
        
        public bool IsUseEpoc { get; set; }
        
        #endregion
        
        #region //--------- Public Commands -------------//
        
        /// <summary>
        /// Gets the browse image command.
        /// </summary>
        /// <value>The browse image command.</value>
        public ICommand BrowseImageCommand
        {
            get
            {
                return new RelayCommand(BrowseImageExecute);
            }
        }
        
        public ICommand NextNavigationCommand
        {
            get
            {
                return new RelayCommand(NextNavigationExecute, CanNextNavigationExecute);
            }
        }
        
        #endregion
        
        #region //--------- Private Methods -------------//
        
        /// <summary>
        /// Browses the image execute.
        /// </summary>
        private void BrowseImageExecute()
        {
            SelectedImagePath = _dialog.ShowDialog(".png", "Images Files|*.png");
        }
        
        /// <summary>
        /// Determines whether can navigate to next view or not.
        /// </summary>
        /// <returns></returns>
        private bool CanNextNavigationExecute()
        {
            return true;
            //return !String.IsNullOrEmpty(SelectedImagePath);
        }
        
        /// <summary>
        /// Nexts the navigation execute.
        /// </summary>
        private void NextNavigationExecute()
        {
            UriQuery query = new UriQuery();
            query.Add("SelectedImagePath", SelectedImagePath);
                
            _regionManager.RequestNavigate(
                RegionNames.MainRegionName,
                new Uri("MoodView" + query.ToString(), UriKind.Relative));
        }

        #endregion

    }
}